public class InterruptTest {
    public static void main(String[] args) throws Exception{
        Thread thread = new Thread() {
            @Override
            public void run() {
                long i = 1L;
                while (true) {
                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println("Interruted!");
                        break;
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(i++);
                }
            }
        };
        thread.start();
        Thread.sleep(2000);
        thread.interrupt();
    }
}